﻿using B.S.BaseData.Api.Write.Applicantion.Command.GoodsCommand;
using B.S.BaseData.Api.Write.GetCelim;
using B.S.BaseData.ErrorCode;
using B.S.BaseData.Infrastructure.Interface;
using B.S.BataBase.Domain.Entitys;
using MediatR;
using Yitter.IdGenerator;

namespace B.S.BaseData.Api.Write.Applicantion.CommandHandler.GoodsCommandHandler
{
    public class GoodshealthCommandHandler : IRequestHandler<GoodshealthCommand, APIResult<int>>
    {
        private readonly IBaseRepository<GoodshealthInfo> _goodshealrepository;
        private readonly IIdContextCemil _iidContextCemil;
        public GoodshealthCommandHandler(IBaseRepository<GoodshealthInfo> goodshealrepository, IIdContextCemil iidContextCemil)
        {
            _goodshealrepository = goodshealrepository;
            _iidContextCemil = iidContextCemil;
        }
        public async Task<APIResult<int>> Handle(GoodshealthCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();
            try
            {
                GoodshealthInfo info = new GoodshealthInfo();
                info. Id= YitIdHelper.NextId();
                info.CreateName = _iidContextCemil.UserName;
                info.GoodshealthDay = request.GoodshealthDay;
                info.Goodshealthmethod = request.Goodshealthmethod;
                info.Goodshealthoperate = request.Goodshealthoperate;
                info.GoodsId = request.GoodsId;
                info.IsDeleted = false;
                result.Code = ApiEnum.Success;
                result.Msg = "保健信息添加成功";
                result.Data = await _goodshealrepository.CreateAsync(info);
                return await Task.FromResult(result);
            }
            catch (Exception)
            {
                result.Code = ApiEnum.Fail;
                result.Msg = "保健信息添加失败";
                return await Task.FromResult(result);
                throw;
            }
        }
    }
}
